package util

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"
)

var GClient *http.Client

func init()  {
	GClient =&http.Client{
		//设置请求超时
		Timeout: time.Second * time.Duration(20),
	}
}

//生成client 通过client的Do方法执行
func HttpPost(url string, data url.Values, timeout int) []byte {

	if timeout > 0{
		GClient = &http.Client{
			//设置请求超时
			Timeout: time.Second * time.Duration(timeout),
		}
	}

	request, err := http.NewRequest("POST", url, strings.NewReader(data.Encode())) //提交请求;用指定的方法，网址，可选的主体放回一个新的*Request
	if err != nil {
		panic(err)
	}

	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	resp, err := GClient.Do(request) //前面预处理一些参数，状态，Do执行发送；处理返回结果;Do:发送请求,
	if err != nil {
		panic(err)
	}
	if resp != nil {
		defer resp.Body.Close()
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	return body
}

func HttpGetCookie(url string, cookiestr string,timeout int) []byte {

	if timeout > 0{
		GClient = &http.Client{
			//设置请求超时
			Timeout: time.Second * time.Duration(timeout),
		}
	}
	//client := &http.Client{
	//	//设置请求超时
	//	Timeout: time.Second * time.Duration(5),
	//}
	request, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println("HttpGetCookie0",err.Error())
		panic(err)
	}
	if request != nil{
		request.Header.Set("Cookie", cookiestr)
		request.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
	}


	resp, err := GClient.Do(request) //前面预处理一些参数，状态，Do执行发送；处理返回结果;Do:发送请求,
	if err != nil {
		fmt.Println("HttpGetCookie1",err.Error())
		panic(err)
	}
	if resp != nil{
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			fmt.Println("HttpGetCookie02",err.Error())
			panic(err)
		}
		return body
	}

	return nil
}

//get
func HttpGet(url string, timeout int,cookiestr string) []byte {
	if timeout > 0{
		GClient = &http.Client{
			//设置请求超时
			Timeout: time.Second * time.Duration(timeout),
		}
	}

	request, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println("HttpGet0",err.Error())
		panic(err)
	}

	if request != nil{
		request.Header.Set("Cookie", cookiestr)
	}


	resp, err := GClient.Do(request) //前面预处理一些参数，状态，Do执行发送；处理返回结果;Do:发送请求,
	if err != nil {
		fmt.Println("HttpGet1",resp.StatusCode)
		panic(err)
	}
	if resp != nil{
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			panic(err)
			panic(err)
		}
		return body
	}


	return nil

}

func HttpGetProxy(client *http.Client, url string, cookiestr string) []byte {

	request, err := http.NewRequest("GET", url, nil)


	if err != nil {
		fmt.Println("HttpGetProxy0",err.Error())
		panic(err)
	}

	request.Header.Set("Cookie", cookiestr)
	request.Header.Set("Proxy-Switch-Ip", "yes")


	resp, err := client.Do(request) //前面预处理一些参数，状态，Do执行发送；处理返回结果;Do:发送请求,

	if err != nil {

		fmt.Println("HttpGetProxy1",err.Error())
		panic(err)
	}

	if resp != nil{
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			panic(err)
		}
		return body
	}

	return nil



}
